<template>
	<view class="indexview">
		<view class="topBar">
			<hx-navbar :background-color="navbarConfig.background" :fixed="true" :back="false" transparent="show" :color="navbarConfig.color"
			 title="城乡物流">
				<navigator class="min-w" slot="left" url="/pages/ex/index">货主</navigator>
				<view class="min-w" slot="right" @tap="show = !show">
					<u-icon name="edit-pen-fill" color="#fff" size="20"></u-icon> 筛选
				</view>
			</hx-navbar>
		</view>
		<u-top-tips ref="uTips" :navbar-height="navbar"></u-top-tips>

		<block v-if="show">
			<view class="filter-popup">
				<view class="filter-box">
					<h3 class="filter-ttl">装货时间</h3>
					<radio-group @change="radioChange1" class="radio-group">
						<label v-for="(item, index) in filterList1" :key="index" class="radio-moni" :class="item.checked ? 'radio-moni-checkd' : ''">
							<radio :color="colorConfig.base" :value="item.value" :checked="item.checked" />
							<view class="value">{{item.name}}</view>
						</label>
					</radio-group>
				</view>
				<view class="filter-box">
					<h3 class="filter-ttl">重量范围：吨</h3>
					<radio-group @change="radioChange2" class="radio-group">
						<label v-for="(item, index) in filterList2" :key="index" class="radio-moni" :class="item.checked ? 'radio-moni-checkd' : ''">
							<radio :color="colorConfig.base" :value="item.value" :checked="item.checked" />
							<view class="value">{{item.name}}</view>
						</label>
					</radio-group>
				</view>
				<view class="filter-box">
					<h3 class="filter-ttl">车长</h3>
					<radio-group @change="radioChange3" class="radio-group">
						<label v-for="(item, index) in filterList3" :key="index" class="radio-moni" :class="item.checked ? 'radio-moni-checkd' : ''">
							<radio :color="colorConfig.base" :value="item.value" :checked="item.checked" />
							<view class="value">{{item.name}}</view>
						</label>
					</radio-group>
				</view>
				<view class="filter-box">
					<h3 class="filter-ttl">车型</h3>
					<radio-group @change="radioChange4" class="radio-group">
						<label v-for="(item, index) in filterList4" :key="index" class="radio-moni" :class="item.checked ? 'radio-moni-checkd' : ''">
							<radio :color="colorConfig.base" :value="item.value" :checked="item.checked" />
							<view class="value">{{item.name}}</view>
						</label>
					</radio-group>
				</view>

				<view class="btm-box">
					<u-button class="custom-style-plain" type="primary" :plain="true" @tap="show = false">取消</u-button>
					<u-button class="custom-style" type="primary" @tap="filterLoad()">确定</u-button>
				</view>
			</view>
		</block>
		<block v-else>
			<!-- 轮播 -->
			<u-swiper class="swiper-box" :list="swiperList" :effect3d="true" height="300" name="imgUrl" @click="swiperClick"></u-swiper>
			<u-notice-bar class="notice-bar" mode="vertical" :is-circular="false" :list="list" :close-icon="true" color="#ffffff"
			 :bg-color="colorConfig.base" border-radius="100" @close="list = []"></u-notice-bar>
			<!-- 抢单卡片list -->
			<view class="card-info" v-for="(item, index) of cardList" :key="index">
				<view class="card-info-top">
					<view class="card-info-top-left">
						<text class="card-info-top-left-time">即时</text>
						<text class="card-info-top-left-timeall">{{$util.sliceTime(item.createTime)}} 装货</text>
					</view>
					<view class="card-info-top-right">
						<text class="card-info-top-right-type">专车</text>
						<text class="card-info-top-right-time">{{$util.sliceTime(item.loadingTime)}}更新</text>
					</view>
				</view>
				<view class="card-info-detail">
					<view class="card-info-goto">
						<view class="card-info-goto-1"><text class="zhuang">起</text></view>
						<text class="card-info-goto-2">{{item.startPointName}}</text>
					</view>
					<view class="card-info-goto">
						<view class="card-info-goto-1"><text class="xie">终</text></view>
						<text class="card-info-goto-2">{{item.endPointName}}</text>
					</view>
					<view class="card-info-time">{{item.carType}} | {{item.cargoType }}</view>
					<view class="card-info-lang">据您{{item.distance}}公里</view>
					<view class="card-info-numview">
						<view class="card-info-num">
							<text class="card-info-num-text1">运费：</text>
							<text class="card-info-num-text2">¥{{item.price}}</text>
						</view>
						<view class="card-info-button">
							<u-button class="custom-style" type="primary" shape="circle" @tap="navToOrder(item)">抢单</u-button>
						</view>
					</view>
				</view>
			</view>
			<uni-load-more :status="loading"></uni-load-more>
		</block>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more.vue'
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				navbar: 0,
				show: false,
				list: [],
				swiperList: [],
				cardList: [],
				filterList1: [{
						name: '不限',
						value: null,
						checked: true
					},
					{
						name: '今天',
						value: '=0',
						checked: false
					},
					{
						name: '明天',
						value: '=-1',
						checked: false
					},
					{
						name: '明天以后',
						value: '<-1',
						checked: false
					}
				],
				filterValue1: null,
				filterList2: [{
						name: '不限',
						value: null,
						checked: true
					},
					{
						name: '0-5',
						value: '0-5',
						checked: false
					},
					{
						name: '5-10',
						value: '5-10',
						checked: false
					},
					{
						name: '10-20',
						value: '10-20',
						checked: false
					},
					{
						name: '20-30',
						value: '20-30',
						checked: false
					},
					{
						name: '30-40',
						value: '30-40',
						checked: false
					},
					{
						name: '40以上',
						value: '40-999',
						checked: false
					}
				],
				filterValue2: null,
				filterList3: [{
						name: '不限',
						value: null,
						checked: true
					},
					{
						name: '1.8米',
						value: '1.8',
						checked: false
					},
					{
						name: '2.7米',
						value: '2.7',
						checked: false
					},
					{
						name: '3.2米',
						value: '3.2',
						checked: false
					},
					{
						name: '4.2米',
						value: '4.2',
						checked: false
					},
					{
						name: '6.8米',
						value: '6.8',
						checked: false
					},
					{
						name: '7.6米',
						value: '7.6',
						checked: false
					},
					{
						name: '9.6米',
						value: '9.6',
						checked: false
					},
					{
						name: '13米',
						value: '13',
						checked: false
					},
					{
						name: '17.5米',
						value: '17.5',
						checked: false
					}
				],
				filterValue3: null,
				filterList4: [{
					name: '不限',
					value: null,
					checked: true
				}],
				filterValue4: null,
				page: 1,
				size: 10,
				loading: 'more', //加载更多状态
			}
		},
		onLoad() {
			this.navbar = uni.getSystemInfoSync().statusBarHeight + 44
			this.swiperData()
			this.loadData()
			this.scrollData()
			this.specData()
		},
		//下拉刷新
		onPullDownRefresh() {
			this.loadData('refresh')
		},
		//加载更多
		onReachBottom() {
			this.loadData()
		},
		computed: {
			...mapState(['userInfo'])
		},
		components: {
			uniLoadMore
		},
		methods: {
			swiperData() {
				this.$api.request('POST', '/api/banner/showBanners', {}, false, 'application/x-www-form-urlencoded').then(res => {
					this.swiperList = res.data.result
				})
			},
			swiperClick(index) {
				console.log(this.swiperList[index].ht5Url)
			},
			scrollData() {
				this.$api.request('POST', '/api/user/selectScroll').then(res => {
					this.list = res.data
				})
			},
			specData() {
				this.$api.request('POST', '/api/car/selectCarSpecifications').then(res => {
					res.data.forEach(item => {
						let data = {
							name: item.name,
							value: item.name,
							checked: false
						}
						this.filterList4.push(data)
					})
				})
			},
			//抢单列表
			loadData(type = 'add') {
				if (type == 'add') {
					if (this.loading == 'nomore') {
						return
					}
					this.loading = 'loading'
				} else {
					this.loading = 'more'
				}
				if (type != 'add') {
					this.page = 1
				}
				let data = {
					page: this.page,
					size: this.size,
					loadingTimes: this.filterValue1,
					loadRanges: this.filterValue2,
					carLengths: this.filterValue3,
					carTypes: this.filterValue4
				}
				this.$api.request('POST', '/api/order/selectOrderBySomeCondition', data, false).then(res => {
					let count = 0
					let idArr = []
					if (type != 'add') {
						this.cardList.forEach(item => {
							idArr.push(item.id)
						})
						res.data.orders.filter(item => {
							!idArr.includes(item.id) && count++
						})
						this.cardList = []
					}
					this.loading = this.page < Math.ceil(res.data.allCount / this.size) ? 'more' : 'nomore'
					this.page = this.page + 1
					this.cardList = this.cardList.concat(res.data.orders)
					if (type != 'add') {
						this.$refs.uTips.show({
							title: '刷新成功,更新了' + count + '条数据'
						})
						// this.$api.msg('刷新成功,更新了' + count + '条数据')
						uni.stopPullDownRefresh()
					}
				})
			},
			filterLoad() {
				this.loadData('filter')
				this.show = !this.show
			},
			navToOrder(item) {
				if (!this.userInfo) {
					this.$api.msg('请先登录')
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 500)
				} else {
					uni.navigateTo({
						url: '/pages/index/confirm?id=' + item.id,
					})
				}
			},
			// 选中某个单选框时，由radio时触发
			radioChange1(e) {
				this.filterList1.forEach(item => {
					item.value == e.detail.value ? item.checked = true : item.checked = false
				})
				this.filterValue1 = e.detail.value
			},
			radioChange2(e) {
				this.filterList2.forEach(item => {
					item.value == e.detail.value ? item.checked = true : item.checked = false
				})
				this.filterValue2 = e.detail.value
			},
			radioChange3(e) {
				this.filterList3.forEach(item => {
					item.value == e.detail.value ? item.checked = true : item.checked = false
				})
				this.filterValue3 = e.detail.value
			},
			radioChange4(e) {
				this.filterList4.forEach(item => {
					item.value == e.detail.value ? item.checked = true : item.checked = false
				})
				this.filterValue4 = e.detail.value
			}
		}
	}
</script>

<style lang="less">
	.swiper-box {
		margin: 30upx 0;
	}

	.notice-bar {
		margin: 0 30upx;
	}

	.searchview {
		display: flex;
		align-items: center;
		text-align: center;
		padding: 20upx 0;
		background-color: #E4E7ED;

		.searchviewinfo {
			flex: 1;
		}
	}

	//筛选弹窗
	.filter-popup {
		.filter-ttl {
			margin-bottom: 20upx;
			font-size: 28upx;
		}

		.filter-box {
			overflow: hidden;
			margin: 30upx;
			padding: 30upx 30upx 0;
			background-color: #fff;
		}
	}

	.btm-box {
		margin: 30upx;
		display: flex;
		justify-content: space-between;

		.custom-style {
			margin: 0;
			width: 320upx;
		}

		.custom-style-plain {
			margin: 0;
			width: 320upx;
		}
	}

	.card-info {
		margin: 30upx;
		padding: 30upx;
		background-color: #fff;
		border-radius: 30upx;
		box-shadow: 0px 1px 6px 0px rgba(0, 0, 0, 0.16);

		.card-info-top {
			display: flex;
			align-items: center;
			font-size: 24upx;
			line-height: 36upx;

			.card-info-top-left {
				flex: 1;
				text-align: left;

				.card-info-top-left-time {
					padding: 0 10upx;
					margin-right: 10upx;
					color: #fff;
					border-radius: 24upx;
					background-color: #0dd9bf;
				}

				.card-info-top-left-timeall {
					color: #0dd9bf;
				}
			}

			.card-info-top-right {
				flex: 1;
				text-align: right;

				.card-info-top-right-type {
					margin-right: 10upx;
					padding: 0 10upx;
					color: #fff;
					border-radius: 24upx;
					background-color: #ea8b6f;
				}

				.card-info-top-right-time {
					color: #ea8b6f;
				}
			}
		}

		.card-info-detail {
			line-height: 55upx;
			margin: 20upx 0 0;

			.card-info-goto {
				display: flex;
				font-size: 24upx;
				line-height: 36upx;

				.card-info-goto-1 {
					flex: 0 0 0;

					text {
						margin: 0 20upx 0 0;
					}
				}

				.zhuang {
					padding: 0 0.25em;
					font-size: 24upx;
					color: #fff;
					background-color: #0869ff;
				}

				.xie {
					padding: 0 0.25em;
					color: #fff;
					background-color: #ff3939;
				}

				.card-info-goto-2 {
					font-weight: bold;
					font-size: 28upx;
					line-height: 36upx;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
					word-break: break-all;
				}
			}
		}

		.card-info-time {
			margin-top: 20upx;
			font-size: 24upx;
			line-height: 24upx;
			text-align: left;
		}

		.card-info-lang {
			margin-bottom: 20upx;
			font-size: 24upx;
			line-height: 24upx;
			text-align: right;
		}

		.card-info-numview {
			display: flex;

			.card-info-num {
				flex: 3;
				font-size: 28upx;
				color: #ff3939;
				text-align: left;

				.card-info-num-text1 {
					color: #ff3939;
				}

				.card-info-num-text2 {
					font-weight: bold;
				}
			}

			.card-info-button {
				flex: 1;
				text-align: right;

				.custom-style {
					margin: 0;
					height: 68upx;
					line-height: 68upx;
				}
			}
		}
	}
</style>
